package com.example.myapplicationtest;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class regist extends AppCompatActivity {
    AlertDialog dialog2;
    String path1="https://www.wanandroid.com/user/register";
    AlertDialog dialog;
    Intent intent;
    EditText  et_username;
    EditText  et_password;
    EditText  et_repassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        dialog=new AlertDialog.Builder(this).create();
        intent = new Intent(this,MainActivity.class);
        et_username  =(EditText)findViewById(R.id.et_nummber);
        et_password  =(EditText)findViewById(R.id.et_nummber1);
        et_repassword  =(EditText)findViewById(R.id.et_nummber2);
    }
    public void click4(View v)
    {
        String username = et_username.getText().toString().trim();
        String password=et_password.getText().toString().trim();
        String repassword=et_repassword.getText().toString().trim();

        new Thread(){
            private HttpURLConnection coon1;

            @Override
            public void run() {

                try {
                    URL url=new URL(path1);
                    coon1=(HttpURLConnection)url.openConnection();
                    coon1.setRequestMethod("POST");
                    coon1.setReadTimeout(5000);
                    coon1.setConnectTimeout(5000);
                    coon1.setDoInput(true);
                    coon1.setDoOutput(true);
                    coon1.setUseCaches(false);
                    String data="username="+ URLEncoder.encode(username,"UTF-8")+"&password="+URLEncoder.encode(password,"UTF-8")+"&repassword="+URLEncoder.encode(repassword,"UTF-8");
                    OutputStream out =coon1.getOutputStream();
                    out.write(data.getBytes());
                    out.flush();
                    if(coon1.getResponseCode()==200){
                        InputStream is=coon1.getInputStream();
                        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while((len = is.read(buffer)) != -1)
                        {
                            outStream.write(buffer,0,len);
                        }
                        is.close();
                        byte[] data1 =outStream.toByteArray();;
                        String html = new String(data1, "UTF-8");
                        System.out.println(html);
                        Gson gson=new Gson();
                        LoginBean loginBean = gson.fromJson(html, LoginBean.class);
                        int  errorMsg =loginBean.getErrorCode();
                        if(errorMsg==0){
                            Message msg = new Message();
                            msg.what=1;
                            msg.obj=errorMsg;
                            handler.sendMessage(msg);
                        }else{
                            Message msg1 = new Message();
                            msg1.what=0;
                            msg1.obj=loginBean.getErrorMsg();
                            handler.sendMessage(msg1);

                        }
                    }

                    else{
                        System.out.println("whywhywhywhywhywhywhwywhywwywywyhw");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();

    }
    private Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg){
            if(msg.what==1){
                int  a =(Integer) msg.obj;
                startActivity(intent);

            }else{
             String ss=(String)msg.obj;
                dialog.setTitle("消息提示框");
                dialog.setMessage(ss);
             dialog.show();

            }
        }


    };
}
